#!/bin/bash
#description: install and configure lamp environment on ubuntu16.04
#author: louis
#date: 2018/2/1

#define varibles
SOFTDIR=/usr/local/src
USER="www"
GROUP="www"

H_SOFT="httpd-2.4.29.tar.gz"
H_SOFT_DIR="httpd-2.4.29"
H_URL="http://mirrors.hust.edu.cn/apache//httpd/"
H_PREFIX="/usr/local/apache24"

M_SOFT="mariadb-10.2.6-linux-x86_64.tar.gz"
M_SOFT_DIR="mariadb-10.2.6-linux-x86_64/"
M_NEW_DIR="mysql"
M_URL="http://soft.51yuki.cn/"
M_PREFIX="/usr/local/"
M_DATADIR="/data"

P_SOFT="php-7.1.13.tar.bz2"
P_SOFT_DIR="php-7.1.13"
P_URL="http://mirrors.sohu.com/php/"
P_PREFIX="/usr/local/php7"
if ! id $USER &>/dev/null;then
	sudo useradd -u 3220 -s /sbin/nologin $USER
fi
#update source
sudo apt-get update
#Installation dependent package
sudo apt-get -y install  libcurl3-openssl-dev libexpat1-dev wget  cmake make gcc g++  libxml2-dev libxml2 build-essential bison  libncurses5-dev re2c  autoconf openssl libcurl4-gnutls-dev libssl-dev libmcrypt-dev

#install apache

install_apache(){
	#define variable
	cd $SOFTDIR
	#APR
        APR_SOFT="apr-1.6.3.tar.gz"
        APR_SOFT_DIR="apr-1.6.3"
        APR_URL="http://soft.51yuki.cn/"
        APR_PREFIX="/usr/local/apr"
        #APR-Util
        APR_UTIL_SOFT="apr-util-1.6.1.tar.gz"
        APR_UTIL_SOFT_DIR="apr-util-1.6.1"
        APR_UTIL_URL="http://soft.51yuki.cn/"
        APR_UTIL_PREFIX="/usr/local/apr-util"
        cd /usr/local/src
        [ ! -f ${H_SOFT} ] && sudo wget  ${H_URL}/${H_SOFT}
        [ ! -f ${APR_SOFT} ] && sudo wget ${APR_URL}/${APR_SOFT}
        [ ! -f ${APR_UTIL_SOFT} ] && sudo wget ${APR_UTIL_URL}/${APR_UTIL_SOFT}
	[ ! -f pcre-8.37.tar.gz ] &&  sudo wget http://jaist.dl.sourceforge.net/project/pcre/pcre/8.37/pcre-8.37.tar.gz
        # install pcre
	cd $SOFTDIR && sudo tar xf pcre-8.37.tar.gz && cd pcre-8.37 && sudo  ./configure --prefix=/usr/local/pcre && sudo make && sudo make install
	#install apr
	cd $SOFTDIR
	sudo tar xf ${APR_SOFT} && cd ${APR_SOFT_DIR} && sudo ./configure --prefix=/usr/local/apr && sudo make && sudo  make install
	#install apr-util
	cd $SOFTDIR
       sudo tar xf ${APR_UTIL_SOFT} && cd ${APR_UTIL_SOFT_DIR} && sudo ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && sudo make && sudo make install
	# install apache
	cd $SOFTDIR 

	sudo tar xf ${H_SOFT} && cd ${H_SOFT_DIR} && sudo ./configure --prefix=${H_PREFIX} --with-pcre=/usr/local/pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-module=so --enable-deflate --enable-so --enable-headers  --enable-ssl --enable-cgi --enable-rewrite --with-zlib  --with-mpm=worker --enable-static-support --enable-mods-shared=all 
	if [ $? -ne 0 ];then
        echo "configure apache fails" 
        exit 1
fi
sudo make && sudo make install
if [ $? -ne 0 ];then
        echo "make or make install fail"
        exit 1
fi
#start apache service
	if [ "`sudo ps -ef|grep apache2`" -gt 1 ];then
		echo "apache service is running"
	else
		sudo ${H_PREFIX}/bin/apachectl
	fi
}

install_php(){
    #install  dependent package
    sudo apt-get -y install libjpeg-dev libpng-dev libmcrypt-dev libreadline6 libreadline6-dev libfreetype6-dev libfreetype6 libcurl3-openssl-dev libssl-dev
    sudo ln -s /usr/lib/x86_64-linux-gnu/libssl.so /usr/lib/
    #install php
    cd $SOFTDIR
    [ ! -f ${P_SOFT} ] && sudo wget ${P_URL}/${P_SOFT}
    sudo tar xf ${P_SOFT} && cd ${P_SOFT_DIR} 
sudo ./configure --prefix=${P_PREFIX} --with-config-file-path=${P_PREFIX}/etc --enable-ftp -enable-fpm --with-fpm-user=$USER --with-fpm-group=$GROUP  --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd  --with-iconv-dir --with-gd --with-freetype-dir --with-jpeg-dir --with-png-dir  --enable-xml --disable-rpath --enable-shmop --enable-sysvsem   --enable-mbregex   --enable-pcntl --enable-sockets --with-xmlrpc --enable-soap   --enable-maintainer-zts --enable-inline-optimization   --enable-opcache   --enable-pdo  --with-gettext --enable-mbstring --with-iconv --with-mcrypt --with-mhash --with-openssl --enable-bcmath --with-libxml-dir  --with-curl --with-zlib --enable-zip --enable-bz2 --with-readline  --with-pear   
if [ $? -ne 0 ];then
	echo "configure php fails" 
	exit 1
fi
sudo make && sudo make install
if [ $? -ne 0 ];then
	echo "make or make install fail"
	exit 1
fi

#copy configure file and service scripts 

cd ${P_PREFIX}/etc
[ ! -f php.ini ] && sudo cp ${SOFTDIR}/${P_SOFT_DIR}/php.ini-production ${P_PREFIX}/etc/php.ini
[ ! -f php-fpm.conf ] && sudo cp php-fpm.conf.default php-fpm.conf
cd ${P_PREFIX}/etc/php-fpm.d/ && sudo  cp www.conf.default www.conf
cd /etc/init.d/ && [ ! -f php-fpm ] && sudo cp ${SOFTDIR}/${P_SOFT_DIR}/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
sudo chmod +x /etc/init.d/php-fpm
#start php service
if [ "`ps aux|grep php-fpm|wc -l`" -gt 1 ];then
	echo "php-fpm service is running"
else
	sudo service php-fpm start
	if [ "`sudo ss -tunlp|grep 9000|awk -F "[ :]+" '{print $6}'`" -eq 9000 ];then
		echo "php-fpm service started successful"
	else
		echo "php-fpm service started fails"
	fi
fi

#configure php env variables

echo "PATH=$PATH:${P_PREFIX}/bin">> ~/.bash_profile 
echo "export PATH">> ~/.bash_profile
source ~/.bash_profile 
}
install_mysql(){
	if ! id mysql &>/dev/null;then
		sudo groupadd -g 3225 mysql
		sudo useradd -u 3225 -g mysql -s /sbin/nologin mysql
	fi
	cd $SOFTDIR
	[ ! -f ${M_SOFT} ] && sudo wget ${M_URL}/${M_SOFT}
	sudo tar xf ${M_SOFT} -C ${M_PREFIX}
	cd ${M_PREFIX}
	sudo ln -s ${M_SOFT_DIR} ${M_NEW_DIR}
	# configure mariadb
	sudo chown -R root.mysql ${M_PREFIX}/${M_NEW_DIR}
	# create mysql data dir
	[ ! -d ${M_DATADIR} ] && sudo mkdir ${M_DATADIR} && sudo chown -R mysql.mysql ${M_DATADIR}
	#configure my.cnf
	sudo cat > /etc/my.cnf << EOF
		[mysqld]
		port = 3306
		socket = /tmp/mysql.sock
		skip-external-locking
		key_buffer_size = 256M
		max_allowed_packet = 1M
		table_open_cache = 256
		sort_buffer_size = 1M
		read_buffer_size = 1M
		read_rnd_buffer_size = 4M
		myisam_sort_buffer_size = 64M
		thread_cache_size = 8
		query_cache_size= 16M
		datadir=${M_DATADIR}
		innodb_log_file_size = 1G
		log_error = error.log
		skip_name_resolve = 1
EOF
 	#Initialize mysql packets
	cd ${M_PREFIX}/${M_NEW_DIR} && sudo scripts/mysql_install_db --user=mysql --datadir=${M_DATADIR} --basedir=${M_PREFIX}/${M_NEW_DIR} 
	sudo chown -R mysql.mysql ${M_DATADIR}
	# configure mariadb service
	sudo cp support-files/mysql.server /etc/init.d/mysqld
	sudo chmod +x /etc/init.d/mysqld
	echo "PATH=$PATH:${M_PREFIX}/${M_NEW_DIR}/bin">> ~/.bash_profile    
	echo "export PATH">> ~/.bash_profile
	source ~/.bash_profile
	#start mysql server
	sudo service mysqld start
}

main() {
#	install_apache
	install_php
	install_mysql
}
main

